<?php

namespace app\forum\controller;


class Post extends Base
{
    protected $postModel = null;

    public function initialize()
    {
        $this->postModel = model('ForumPost');
        parent::initialize();
    }

    public function index()
    {
        $bid = input('bid');

        $list = $this->postModel->getPostListByBid($bid);

        $typeList = model('ForumType')->getListByBid($bid);
        $this->assign('typeList', $typeList);

        $this->assign('list', $list);
        $this->assign('bid', $bid);
        return view('index');
    }


    public function detail()
    {
        $id = input('id');
        $detail = $this->postModel->getPost($id);
        $this->assign('detail', $detail);
        return view('detail');
    }


    public function add()
    {
        $bid = input('bid');
        if (!is_login()) {
            $this->error('请登录后再操作', url('user/index/login', ['refer' => 1]));
        }

        if (request()->isPost()) {
            $title = input('title', '', 'text');
            $content = input('content', '', 'html');
            $type = input('type', 0, 'intval');
            $uid = is_login();
            $data = [
                'uid' => $uid,
                'bid' => $bid,
                'type' => $type,
                'title' => $title,
                'content' => $content,
            ];
            $rs = $this->postModel->addPost($data);
            if ($rs > 0) {
                $this->success('发布成功');
            } else {
                $this->error('发布失败');
            }

        } else {

            $typeList = model('ForumType')->getListByBid($bid);
            $this->assign('bid', $bid);
            $this->assign('typeList', $typeList);
            return view('add');
        }


    }

}
